admin-menu-fix.js ➔ menuFix   A
last analyzed

Complexity

Conditions 5

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
dl 0
loc 27
rs 9.0833
c 0
b 0
f 0
1
/**
2
 * As we are using hash based navigation, hack fix
3
 * to highlight the current selected menu
4
 *
5
 * Requires jQuery
6
 */
7
function menuFix(slug) {
8
    var $ = jQuery;
9
10
    let menuRoot = $('#toplevel_page_' + slug);
11
    let currentUrl = window.location.href;
12
    let currentPath = currentUrl.substr( currentUrl.indexOf('admin.php') );
13
14
    menuRoot.on('click', 'a', function() {
15
        var self = $(this);
16
17
        $('ul.wp-submenu li', menuRoot).removeClass('current');
18
19
        if ( self.hasClass('wp-has-submenu') ) {
20
            $('li.wp-first-item', menuRoot).addClass('current');
21
        } else {
22
            self.parents('li').addClass('current');
23
        }
24
    });
25
26
    $('ul.wp-submenu a', menuRoot).each(function(index, el) {
27
        if ( $(el).attr( 'href' ) === currentPath ) {
28
            $('ul.wp-submenu a', menuRoot).parent().removeClass('current');
29
            $(el).parent().addClass('current');
30
            return;
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
31
        }
32
    });
33
}
34
35
export default menuFix;